package com.winit.util;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class ConfigUtil {

    private static Properties prop;
    private static final String propFile = "config.properties";

    private ConfigUtil() {

    }

    /**
     * 初始化配置文件
     */
    public static synchronized void initPropertFile() {
        if (prop == null) {
            prop = new Properties();
            try {
                InputStream ins = Thread.currentThread().getContextClassLoader().getResourceAsStream(propFile);
                prop.load(new InputStreamReader(ins, "UTF-8"));
            } catch (Exception e) {
                System.out.println("加载Config.properties 文件出错:" + e);
            }
        }
    }

    public static void reloadPropertFile() {
        try {
            prop = new Properties();
            InputStream ins = Thread.currentThread().getContextClassLoader().getResourceAsStream(propFile);
            prop.load(new InputStreamReader(ins, "UTF-8"));
        } catch (Exception e) {
            System.out.println("加载Config.properties 文件出错:" + e);
        }
    }

    /**
     * @param value
     * @return
     */
    public synchronized static String getValue(String value) {
        ConfigUtil.initPropertFile();
        return prop.getProperty(value);
    }

    public synchronized static void setValue(String key, String value) {
        ConfigUtil.initPropertFile();
        prop.setProperty(key, value);
    }

}
